var http_request_sel = initRequest();
var sever_path_local=getContextPath();
var localStorage=storagObj();

function initRequest() {
	var request = false;
	if(window.XMLHttpRequest) { //FireFox
		request = new XMLHttpRequest();
		if(request.overrideMimeType) {
			request.overrideMimeType('text/xml');
		}
	}
	if(!request) {
		window.alert("Create request error!");
		return false;
	}
	return request;
}

function send(sendUrl, sendData, ajax_call_back, type) {

	// sendUrl += "&random="+Math.random();   //在URL后添加这个，可以保证不被缓存

	http_request_sel.onreadystatechange = ajax_call_back; //指定请求返回时的回调函数
	//post
	if(type == 1) {
		ajaxPost(sendUrl, sendData);
	} else if(type == 2) {
		ajaxgest(sendUrl, sendData)
	} else {
		ajaxgest(sendUrl)
	}

}

function ajaxPost(sendUrl, sendData) {
	http_request_sel.open("POST", sendUrl, true);
	http_request_sel.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	http_request_sel.send(sendData);
}

function ajaxgest(sendUrl) {
	http_request_sel.open("GET", sendUrl, true);
	http_request_sel.send(null);
}

function ajaxgest(sendUrl, sendData) {
	http_request_sel.open("GET", sendUrl, true);
	http_request_sel.send(sendData);
}

//	function ajax_call_back(data) {
//		alert("111");
//		var sel = document.getElementById("sn1");
//		sel.options.length = 0;
//		//readyState共有5中状态，0未初始化，1已初始化，2发送请求，3开始接收结果，4接收结果完毕。
//		//status服务器响应状态码
//		if(http_request_sel.readyState == 4) {
//			if(http_request_sel.status == 200) {
//				var str = http_request_sel.responseText;
//				//alert("返回数据为：" +  JSON.parse(str));
//				//var list=JSON.parse(str);
//				var list = eval(str);
//				for(var i = 0; i < list.length; i++) {
//					var op = document.createElement("option");
//					op.text = list[i].countyName; //显示值
//					op.value = list[i].countyName; //value
//					sel.add(op);
//				}
//			} else if(http_request_sel.status == 404) {
//				alert("请求资源不存在！");
//			} else {
//				alert("数据请求失败，错误状态为：" + http_request_sel.status);
//			}
//		}
//	}

/**
 * 访问地址
 */
function getContextPath() {
	// 获取当前网址，如：http://localhost:8080/ssm/index.jsp
	var currentPath = window.document.location.href;
	// 获取主机地址之后的目录，如： /ssm/index.jsp
	var pathName = window.document.location.pathname;
	var pos = currentPath.indexOf(pathName);
	// 获取主机地址，如： http://localhost:8080
	var localhostPath = currentPath.substring(0, pos);
	// 获取带"/"的项目名，如：/ssm
	var projectName = pathName.substring(0, pathName.substr(1).indexOf('/') + 1);
	// 	    alert("currentPath:" + currentPath + "\n"
	// 	            + "pathName:" + pathName + "\n"
	// 	            + "localhostPath:" + localhostPath + "\n"
	// 	            + "projectName:" + projectName + "\n"
	// 	            + "contextPath:"+ localhostPath + projectName);
	return(localhostPath + projectName);
}

var username = document.cookie.split(";")[0].split("=")[1];
//JS操作cookies方法!
//写cookies
function setCookie(name, value) {
	var Days = 30;
	var exp = new Date();
	exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);
	document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString();
}

function getCookie(name) {
	var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
	if(arr = document.cookie.match(reg))
		return unescape(arr[2]);
	else
		return null;
}

function delCookie(name) {
	var exp = new Date();
	exp.setTime(exp.getTime() - 1);
	var cval = getCookie(name);
	if(cval != null)
		document.cookie = name + "=" + cval + ";expires=" + exp.toGMTString();
}

//如果需要设定自定义过期时间
//那么把上面的setCookie　函数换成下面两个函数就ok;
//程序代码
function setCookie(name, value, time) {
	var strsec = getsec(time);
	var exp = new Date();
	exp.setTime(exp.getTime() + strsec * 1);
	document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString();
}

function getsec(str) {
	var str1 = str.substring(1, str.length) * 1;
	var str2 = str.substring(0, 1);
	if(str2 == "s") {
		return str1 * 1000;
	} else if(str2 == "h") {
		return str1 * 60 * 60 * 1000;
	} else if(str2 == "d") {
		return str1 * 24 * 60 * 60 * 1000;
	}
}
//这是有设定过期时间的使用示例：
//s20是代表20秒
//h是指小时，如12小时则是：h12
//d是天数，30天则：d30
//setCookie("name", "hayden", "s20");
//使用示例
//setCookie("name", "hayden");
//alert(getCookie("name"));

function storagObj(){
	// Check browser support
	if (typeof(Storage) !== "undefined") {
	    // Store
//	    localStorage.setItem("lastname", "Gates");
	    // Retrieve
//	    alert(localStorage.getItem("lastname"))
        return localStorage;
	} 
}


function GetQueryString(name)
{
     var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
     var r = window.location.search.substr(1).match(reg);
     if(r!=null)return  unescape(r[2]); return null;
}
/**
 * localStorage.getItem('key')
 * @param {Object} data
 */
 function getJsonObj(data){
  	     var userstr=JSON.stringify(data);
		return JSON.parse(userstr);
  }

